﻿using System;
using System.Data;
using System.Data.Common;
using NUnit.Framework;
using Sucdri.Infrastructure.Core.DbUtility;

namespace Sucdri.Infrastructure.Core.Test.DBUtility
{
   [TestFixture]
   public class DbUtilityTest
   {
      private DbHelper _dbHelper;

      [TestFixtureSetUp]
      public void FixtureSetUp()
      {
         _dbHelper = DbFactory.GetDbHelper();
      }

      [TestFixtureTearDown]
      public void FixtureTearDown()
      {
         _dbHelper = null;
      }

      [Test]
      public void CanConnectToDefaultDataBase()
      {
         using (DbConnection connection = _dbHelper.DbConnection)
         {
            connection.Open();
            Assert.AreEqual(connection.State, ConnectionState.Open);
         }
      }

      [Test]
      public void CanCreateDbHelper()
      {
         Assert.IsNotNull(_dbHelper);
      }

      [Test]
      public void CanCreateTransaction()
      {
         TransactionManager.BeginTransaction();
         TransactionManager.Commit();
      }

      [Test]
      public void ExecptionWillbeThrownIfDatabaseNameIsEmpty()
      {
         Assert.Throws(typeof (ArgumentException), () => DbFactory.GetDbHelper(""));
      }

      [Test]
      public void ExecptionWillbeThrownIfTransactionOpenTwice()
      {
         Assert.Throws(typeof (InvalidOperationException), () =>
                                                              {
                                                                 TransactionManager.BeginTransaction();
                                                                 TransactionManager.BeginTransaction();
                                                              });
      }
   }
}